package com.teamall.web.main.controller;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.JsonUtil;
import com.teamall.framework.util.MD5Utils;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.main.db.po.CodeStatTownPO;
import com.teamall.web.main.service.ICodeStatTownService;
import com.teamall.web.main.vo.LoginUserInfo;
import com.teamall.web.sys.db.po.SysInfoPO;
import com.teamall.web.sys.db.po.SysUserPO;
import com.teamall.web.sys.service.ISysInfoService;
import com.teamall.web.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-28 22:43
 **/
@Controller
@RequestMapping("/main")
public class MainController {
    @Autowired
    private ICodeStatTownService codeStatTownService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysInfoService sysInfoService;
    @RequestMapping("index")
    public ModelAndView index(HttpServletRequest request) throws BaseAppException {
        LoginUserInfo loginUserInfo = (LoginUserInfo) request.getSession().getAttribute("loginUserInfo");
        ModelAndView mv = new JModelAndView("index");
        return mv;
    }

    @RequestMapping("indexIframe")
    public ModelAndView indexIframe(HttpServletRequest request) throws BaseAppException {
        LoginUserInfo loginUserInfo = (LoginUserInfo) request.getSession().getAttribute("loginUserInfo");
        ModelAndView mv = new JModelAndView("index_i");
        List<CodeStatTownPO> pkrkList = codeStatTownService.selectByArg(new CodeStatTownPO());
        SysInfoPO sysInfoPO = new SysInfoPO();
        sysInfoPO.setInfoType("1");
        Page<SysInfoPO> page = new Page<SysInfoPO>();
        page.setRows(4);
        sysInfoService.selectByArgAndPage(sysInfoPO,page);
        String pkListStr = JsonUtil.toJson(pkrkList);
        mv.addObject("pkListStr", pkListStr);
        mv.addObject("pkrkList", pkrkList);
        mv.addObject("infoList", page.getResultList());
        mv.addObject("userName", loginUserInfo.getUserName());
        return mv;
    }
    @RequestMapping("logout")
    public ModelAndView logout() {
        System.out.printf("5555");
        ModelAndView mv = new JModelAndView("login");
        return mv;
    }

    @RequestMapping("login")
    @ResponseBody
    public ResResult login(HttpServletRequest request, String userName, String password) throws BaseAppException {
        ResResult resResult = new ResResult();
        String md5password = MD5Utils.MD5(password);
        SysUserPO sysUserPO = new SysUserPO();
        sysUserPO.setPassword(md5password.toLowerCase());
        sysUserPO.setLoginName(userName);
        // sysUserPO.setUserObjType(2);
        List<SysUserPO> sysUserPOList = sysUserService.selectByArg(sysUserPO);
        if (sysUserPOList.size() > 0) {
            LoginUserInfo loginUserInfo = new LoginUserInfo();
            loginUserInfo.setUserName(sysUserPOList.get(0).getUserName());
            loginUserInfo.setUserId(sysUserPOList.get(0).getUserId());
            loginUserInfo.setUserObjId(sysUserPOList.get(0).getUserObjId());
            request.getSession().setAttribute("loginUserInfo", loginUserInfo);
        } else {
            resResult.setCode("-1");
            resResult.setMsg("用户名或者密码不正确");
        }
        return resResult;
    }
}
